<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="umsroot.css">
<TITLE>
Array Notation
</TITLE>
</HEAD>
<BODY >
<A HREF="umsroot023.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot021.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot025.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc51">5.3</A>&nbsp;&nbsp;Array Notation</H2><UL>
<LI><A HREF="umsroot024.html#toc29">Implementation Note</A>
</UL>

<A NAME="@default170"></A>
<A NAME="@default171"></A>

Since our language has no type declarations, there is really
no difference between a structure and an array. In fact,
a structure can always be used as an array, creating it with
<A HREF="../bips/kernel/termmanip/functor-3.html"><B>functor/3</B></A><A NAME="@default172"></A>
and accessing elements with
<A HREF="../bips/kernel/termmanip/arg-3.html"><B>arg/3</B></A><A NAME="@default173"></A>.
However, this can look clumsy, especially in arithmetic expressions.<BR>
<BR>
ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> therefore provides array syntax which enables the
programmer to write code like
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[eclipse 1]: Prime = a(2,3,5,7,11), X is Prime[2] + Prime[4].
X = 10
Prime = a(2, 3, 5, 7, 11)
yes.
</PRE></BLOCKQUOTE>
Within expressions, array elements can be written as variable-indexlist
or structure-indexlist sequences, e.g.
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
X[3] + M[3,4] + s(4,5,6)[3]
</PRE></BLOCKQUOTE>
Indices run from 1 up to the arity of the array-structure.
The number of array dimensions is not limited.<BR>
<BR>
To create multi-dimensional arrays conveniently, the built-in
<A HREF="../bips/kernel/termmanip/dim-2.html"><B>dim/2</B></A><A NAME="@default174"></A>
is provided (it can also be used backwards to access
the array dimensions):
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[eclipse]: dim(M,[3,4]), dim(M,D).
M = []([](_131, _132, _133, _134),
       [](_126, _127, _128, _129),
       [](_121, _122, _123, _124))
D = [3, 4]
yes.
</PRE></BLOCKQUOTE>
Although
<A HREF="../bips/kernel/termmanip/dim-2.html"><B>dim/2</B></A><A NAME="@default175"></A>
creates all structures with the functor [&nbsp;], this has
no significance other than reminding the programmer that
these structures are intended to represent arrays.<BR>
<BR>
Array notation is especially useful within loops.
Here is the code for a matrix multiplication routine:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
matmult(M1, M2, M3) :-
        dim(M1, [MaxIJ,MaxK]),
        dim(M2, [MaxK,MaxIJ]),
        dim(M3, [MaxIJ,MaxIJ]),
        (
            for(I,1,MaxIJ),
            param(M1,M2,M3,MaxIJ,MaxK)
        do
            (
                for(J,1,MaxIJ),
                param(M1,M2,M3,I,MaxK)
            do
                (
                    for(K,1,MaxK),
                    fromto(0,Sum0,Sum1,Sum),
                    param(M1,M2,I,J)
                do
                    Sum1 is Sum0 + M1[I,K] * M2[K,J]
                ),
                subscript(M3, [I,J], Sum)
            )
        ).
</PRE></BLOCKQUOTE>
<A NAME="@default176"></A>
<A NAME="toc29"></A>
<H3 CLASS="subsection"><A NAME="htoc52">5.3.1</A>&nbsp;&nbsp;Implementation Note</H3>
Array syntax is implemented by parsing variable-list and
structure-list sequences as terms with the functor subscript/2.
<A NAME="@default177"></A>
For example:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
X[3]          ---&gt;      subscript(X, [3])
M[3,4]        ---&gt;      subscript(M, [3,4])
s(4,5,6)[3]   ---&gt;      subscript(s(4,5,6), [3])
</PRE></BLOCKQUOTE>
If such a term is then used within an arithmetic expression,
a result argument is added and the built-in predicate
<A HREF="../bips/kernel/termmanip/subscript-3.html"><B>subscript/3</B></A><A NAME="@default178"></A>
is called, which is a generalised form of
<A HREF="../bips/kernel/termmanip/arg-3.html"><B>arg/3</B></A><A NAME="@default179"></A>
and extracts the indicated array element.<BR>
<BR>
When printed, subscript/2 terms are again printed in array notation,
unless the print-option to suppress operator notation ("O") is used.<BR>
<BR>
<HR>
<A HREF="umsroot023.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot021.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot025.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
